<?php
/**
 * @author GhostWei
 * @date Sep 21, 2012
 */
class oauth_open_google extends oauth_api_abstract {

	protected $_name = 'google';
	protected $_url = array(
			'authorize' => 'https://accounts.google.com/o/oauth2/auth',
			'token' => 'https://accounts.google.com/o/oauth2/token',
			'user_info' => 'https://www.googleapis.com/oauth2/v1/userinfo'
	);
	protected $scope = array(
			'https://www.googleapis.com/auth/userinfo.profile',
			//'https://www.googleapis.com/auth/userinfo.email'
	);
	/**
	 * @return oauth_api_response
	 */
	public function request($action = null, $params = array()) {
		$response = $this->getOauthResponse();
		if(empty($params)) {
			$oauth = $this->getSession()->getToken();
			if(!(isset($oauth['access_token']))) {
				return $response->addError('access token is empty or invalid');
			}
			$params = array('access_token' => $oauth['access_token']);
		}
		if($action === null) {
			$url = $this->_url['user_info'];
		} elseif(is_string($action)) {
			$url = $action;
		} else {
			return $response->addError('invalid request');
		}
		$symbol = strpos($url, '?') ? '&' : '?';
		if(!($body = $this->get($url . $symbol . http_build_query($params)))) {
			return $response->addError('HTTP RESPONSE IS NOT OK');
		}
		return $response->setData($body);
	}
	
	/**
	 * @return oauth_open_qzone
	 */
	public function authorize() {
		$request = $this->getRequest();
		if(1 && $error = $request->getQuery('error')) {
			return $this->addError($error);
		}
		$code = $request->getQuery('code');
		$state = $request->getQuery('state');
		if($state && $this->getSession()->getState() != $state) {
			return $this->addError('state invalid');
		}
		/**
		 * 获取 access token
		 */
		$p = array();
		$p['code'] = $code;
		$p['client_id'] = $this->getSource()->getAppId();
		$p['client_secret'] = $this->getSource()->getAppKey();
		$p['grant_type'] = 'authorization_code';
		$p['redirect_uri'] = $this->getRedirectUrl(false);
		if(!($response = $this->post($this->_url['token'] . '?' . http_build_query($p), $p))) {
			return $this->addError('HTTP RESPONSE IS NOT OK');
		}
		if(!($json = $this->parseJson($response))) {
			return $this;
		}
		$this->getSession()->setToken(array(
				'access_token' => $json->access_token, 
				'app_id' => $p['client_id'], 
				'app_key' => $json->token_type,
				'expires' => $json->expires_in
		));
		return $this;
	}
	
	public function url($g_ut = null) {
		$p = array();
		$p['response_type'] = 'code';
		$p['client_id'] = $this->getSource()->getAppId();
		$p['redirect_uri'] = $this->getRedirectUrl(false);
		$p['state'] = $this->getSession()->setState(md5(time()))->getState();
		$p['scope'] = implode(' ', $this->scope);
		$p['access_type'] = 'offline';
		$p['approval_prompt'] = 'auto';
		return $this->_url['authorize'] . '?' . http_build_query($p);
	}


	private function parseJson($response) {
		$json = trim($response);
		try {
			return json_decode($json);
		} catch (Exception $e) {
			$this->addError($e->getMessage());
		}
		return null;
	}
	
}
